package Java7_Concurrency_Cookbook.concurrent_collection_framework.linked_blocking_deque;

import java.util.Date;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * LinkedBlockingDeque.put()如果列表已满, 线程将阻塞, 直到列表有可用空间<br/>
 * LinkedBlockingDeque.take(), takeFirst(), takeLast() 返回的元素会从列表中删除, 如果列表为空,线程将阻塞直到列表不为空<br/>
 *
 * LinkedBlockingDeque.peek(), peekFirst(), peekLast() 返回的元素不会从列表中删除, 如果列表为空, 返回 NULL<br/>
 * LinkedBlockingDeque.poll(), pollFirst(), pollLast() 返回的元素会从列表中删除, 如果列表为空, 返回 NULL<br/>

 * LinkedBlockingDeque.add(), addFirst(), addLast() 如果列表已满, 将抛出异常<br/>
 * LinkedBlockingDeque.getFirst(), getLast() 返回的元素不会从列表中删除, 如果列表为空, 将抛出异常<br/>
 *
 * take	阻塞
 * get	抛异常
 * peek	返回 NULL
 *
 * remove	抛异常	删除元素
 * poll(timeout)	返回 NULL	删除元素
 *
 * add	抛异常
 * offer(timeout)	boolean
 * put	阻塞
 *
 * void push(E e);	addFirst
 * E pop();	removeFirst
 *
 *
 * @author lixiangke
 * @date 2017/11/23
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingDeque<String> list = new LinkedBlockingDeque<>(3);
        Client client = new Client(list);
        Thread thread = new Thread(client);
        thread.start();

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                String request = list.take();
                System.out.printf("Main: Request %s at %s. Size: %d\n", request, new Date(), list.size());
            }
            TimeUnit.MILLISECONDS.sleep(300);
        }

        System.out.printf("Main: End of the program. \n");
    }
}
